package com.fuelwallet.view;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.fuelwallet.R;
import com.fuelwallet.model.CarProfile;
import com.fuelwallet.model.CarProfileHandle;
import com.fuelwallet.model.CurrentAccount;
import com.fuelwallet.model.FuelPriceHandle;
import com.fuelwallet.model.HistoryHandle;

public class ShowReportActivity extends Activity {
	// Debugging
	private static final String TAG = "ShowReportActivity";
	private static final boolean D = true;
	
	// Intent Key name
	public static final String START_ADDRESS = "start";
	public static final String END_ADDRESS = "end";
	public static final String ROUTE_DURATION = "route_duration";
	public static final String ROUTE_DISTANCE = "route_distance";	
	public static final String ROUTE_DURATION_TOSHOW = "route_duration_toshow";
	public static final String ROUTE_DISTANCE_TOSHOW = "route_distance_toshow";	
	
	// Member view elements	
	private TextView text_from;
	private TextView text_to;
	private TextView text_distance;
	private TextView text_duration;
	private TextView text_fuel_type;
	private TextView text_unit_price;
	private TextView text_car_disp;
	private TextView text_est_cost;
	private Spinner spinner;
	private ImageView image_show_map;
	private Button btn_record;
	private Button btn_direction;
	
	private int route_distance; 	
	private double estCost;
	private CarProfileHandle carHandler;
	private CurrentAccount currentAccHandler;
	private CarProfile currentCar;
	private FuelPriceHandle fuelPriceHandler;
	private HistoryHandle historyUtil;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_report);
		
		image_show_map = (ImageView) findViewById(R.id.imageview_show_map);
		text_from = (TextView) findViewById(R.id.textview_from);
		text_to = (TextView) findViewById(R.id.textview_to);
		text_distance = (TextView) findViewById(R.id.textview_distance);
		text_duration = (TextView) findViewById(R.id.textview_duration);
		text_fuel_type = (TextView) findViewById(R.id.textview_fuel_type);
		text_unit_price = (TextView) findViewById(R.id.textview_unit_price);
		text_car_disp = (TextView) findViewById(R.id.textview_car_displacement);
		text_est_cost = (TextView) findViewById(R.id.textview_est_cost);
		spinner = (Spinner) findViewById(R.id.spinner_car);
		btn_record = (Button) findViewById(R.id.btn_record_cost);
		btn_direction = (Button) findViewById(R.id.btn_new_direction);		
		carHandler = new CarProfileHandle(this);
		currentAccHandler = new CurrentAccount(this);
		historyUtil = new HistoryHandle(this);
		fuelPriceHandler = new FuelPriceHandle(this);
		
		image_show_map.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				ShowReportActivity.this.finish();	
				overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
			}
		});	
		btn_direction.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				Intent intent = new Intent(ShowReportActivity.this, BaseMapActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				intent.putExtra(BaseMapActivity.LAUNCH_OPTIONS, BaseMapActivity.OPTION_GET_DIRECTION);
				ShowReportActivity.this.startActivity(intent);
				overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
			}
		});
		btn_record.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				historyUtil.open();
					historyUtil.putHistory(currentCar.getAccountName(), 
						(float)route_distance/1000, (float)estCost);
				historyUtil.close();
				currentAccHandler.open();
					currentAccHandler.setCurrentAccount(currentCar);
				currentAccHandler.close();
				Toast.makeText(ShowReportActivity.this, "Current trip info recorded!", Toast.LENGTH_SHORT).show();
			}
		});		
	}
	
	public void onStart(){
		super.onStart();
		
		// Get car information from database
		carHandler.open();
			List<String> carAccounts = carHandler.getAccountNameList();
		carHandler.close();
		currentAccHandler.open();
			currentCar = currentAccHandler.getCurrentAccount();
		currentAccHandler.close();
		fuelPriceHandler.open();
			float price = fuelPriceHandler.getFuelPrice(currentCar.getFuelName()).getfuelPrice(); 
			//float price = 15.3f;
		fuelPriceHandler.close();

		// Set the spinner
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, carAccounts);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setSelection(carAccounts.indexOf(currentCar.getAccountName()));
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				currentAccHandler.open();
				carHandler.open();
					currentCar = carHandler.getCarProfile((String)parent.getItemAtPosition(position));					
					currentAccHandler.setCurrentAccount(currentCar);
					fuelPriceHandler.open();
						float price = fuelPriceHandler.getFuelPrice(currentCar.getFuelName()).getfuelPrice(); 
						//float price = 15.3f;
					fuelPriceHandler.close();
					updateEstCost(price);	
					text_fuel_type.setText(currentCar.getFuelName());
					text_unit_price.setText(String.format("%.2f HKD/L",price));
					text_car_disp.setText(String.format("%.1f L", currentCar.getDisplacement()));		
					text_est_cost.setText(String.format("%.2f HKD", estCost));	
				carHandler.close();
				currentAccHandler.close();
				
				text_fuel_type.invalidate();
				text_unit_price.invalidate();
				text_car_disp.invalidate();
				text_est_cost.invalidate();
			}

			public void onNothingSelected(AdapterView<?> parent) {
				return;
			}
		});

		Intent intent = getIntent();
		route_distance = intent.getIntExtra(ROUTE_DISTANCE, 0);						// in meters
		String route_duration_toshow = intent.getStringExtra(ROUTE_DURATION_TOSHOW);
		String route_distance_toshow = intent.getStringExtra(ROUTE_DISTANCE_TOSHOW);
		String start = intent.getStringExtra(START_ADDRESS);
		String end = intent.getStringExtra(END_ADDRESS);
		
		// Calculate estimated cost
		updateEstCost(price);			
		text_from.setText(start);
		text_to.setText(end);
		text_distance.setText(route_distance_toshow);
		text_duration.setText(route_duration_toshow);
		
		text_fuel_type.setText(currentCar.getFuelName());
		text_unit_price.setText(String.format("%.2f HKD/L",price));
		text_car_disp.setText(String.format("%.1f L", currentCar.getDisplacement()));		
		text_est_cost.setText(String.format("%.2f HKD", estCost));		
	}
	
	public void onBackPressed(){		
		super.onBackPressed();
		overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
	}
	
	/* using linear interpolation
	 * <0.8	   : 5L/100Km
	 * 0.8-1.3 : 5-7L/100Km 
	 * 1.5-2.0 : 7-9L/100Km 
	 * 2.2-2.8 : 8-13L/100Km 
	 * 3.0-4.2 : 12-15L/100Km
	 * >4.2    : 15L/100Km
	 */
	private void updateEstCost(float price){
		float displacement = currentCar.getDisplacement();
				
		if(displacement <= 0.8){
			estCost = (5.0 * (double)route_distance / 1000) * price;
		}
		else if(displacement > 0.8 && displacement <= 1.3){			// y = 4x + 1.8
			estCost = ((displacement * 4 + 1.8) * (double)route_distance / 1000) * price;
		}
		else if(displacement > 1.3 && displacement <= 1.5){
			estCost = (7.0 * (double)route_distance / 1000) * price;
		}
		else if(displacement > 1.5 && displacement <= 2.0){			// y = 4x + 1
			estCost = ((displacement * 4 + 1) * (double)route_distance / 1000) * price;
		}
		else if(displacement > 2.0 && displacement <= 2.2){
			estCost = (9.0 * (double)route_distance / 1000) * price;
		}
		else if(displacement > 2.2 && displacement <= 2.8){			// y = (25/3)x - 31/3
			estCost = ((displacement * 25/3 - 31/3) * (double)route_distance / 1000) * price;
		}
		else if(displacement > 2.8 && displacement <= 3.0){
			estCost = (13.0 * (double)route_distance / 1000) * price;
		}
		else if(displacement > 3.0 && displacement <= 4.2){			// y = 2.5x + 4.5
			estCost = ((displacement * 2.5 - 4.5) * (double)route_distance / 1000) * price;
		}
		else{//(displacement > 4.2)
			estCost = (15.0 * (double)route_distance / 1000) * price;
		}
		
		estCost /= 100;
		return;
	}
		
}
